package com.skillworld.webapp.web.util;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import com.skillworld.webapp.model.enrollment.ReleasedExam.Grade;
import com.skillworld.webapp.model.tutorial.MarkedTutorial.Mark;
import com.skillworld.webapp.model.tutorial.Tutorial.Status;
import com.skillworld.webapp.model.tutorial.TutorialOrder;

public class Parameter 
{
    public static Boolean maybeBoolean(String booleanParam)
    {
        return (booleanParam != null)
                ? Boolean.parseBoolean(booleanParam)
                : null;
    }
    
    public static Integer maybeInt(String intParam)
    {
        return (intParam != null)
                ? Integer.parseInt(intParam)
                : null;
    }
    
    public static int maybeInt(String intParam, int defaultValue)
    {
        return (intParam != null)
                ? Integer.parseInt(intParam)
                : defaultValue;
    }
    
    public static Long maybeLong(String longParam)
    {
        return (longParam != null)
                ? Long.parseLong(longParam)
                : null;
    }
    
    public static Mark maybeMark(String markParam)
    {
        return (markParam != null)
                ? Mark.valueOf(markParam.toUpperCase())
                : null;
    }
    
    public static int parseRating(String ratingParam)
    {
        int rating = Integer.parseInt(ratingParam);
        
        if (rating < 1 || rating > 5)
        { throw new IllegalArgumentException(); }
        
        return rating;
    }
    
    public static Grade parseGrade(String gradeParam)
    {
        int ordinal = Integer.parseInt(gradeParam);
        
        if (ordinal < 0 || ordinal > 4)
        { throw new IllegalArgumentException(); }
        
        return Grade.values()[ordinal];
    }
    
    public static TutorialOrder maybeTutorialOrder(String orderParam, 
                                                    TutorialOrder defaultValue)
    {
        return (orderParam != null)
                ? TutorialOrder.valueOf(orderParam.toUpperCase())
                : defaultValue;
    }
    
    public static List<String> commaSep(String toksParam)
    {
        List<String> tokens = new ArrayList<String>();
        StringTokenizer tokenizer = new StringTokenizer(toksParam,",");
        
        while (tokenizer.hasMoreTokens())
        { tokens.add(tokenizer.nextToken()); }
        
        return tokens;
    }
    
    public static List<Long> commaSepIds(String idsParam)
    {
        List<Long> ids = new ArrayList<Long>();
        StringTokenizer tokenizer = new StringTokenizer(idsParam,",");
        
        while (tokenizer.hasMoreTokens())
        { ids.add(Long.parseLong(tokenizer.nextToken())); }
        
        return ids;
    }

    public static List<Status> commaSepStatuses(String statusParam)
    {
        List<Status> statuses = new ArrayList<Status>();
        StringTokenizer tokenizer = new StringTokenizer(statusParam,",");
        
        while (tokenizer.hasMoreTokens())
        { statuses.add(Status.valueOf(tokenizer.nextToken().toUpperCase())); }
        
        return statuses;
    }
}
